﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VOL;
using BLL;
using System.Data;
using System.Data.SqlClient;


namespace WebShoes.Admin
{
    public partial class NguoiQuanLyForm : System.Web.UI.Page
    {
        NguoiQuanLyBL _NguoiQuanLyBL = new NguoiQuanLyBL();
        int maNguoiQuanLy = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string _maNguoiQuanLy = Request.QueryString["MaNguoiQuanLy"];
                if (_maNguoiQuanLy == null || _maNguoiQuanLy == "" || !int.TryParse(_maNguoiQuanLy, out maNguoiQuanLy))
                {
                    hdfMaNguoiQuanLy.Value = "0";
                }
                else
                {
                    DataTable dt = _NguoiQuanLyBL.searchIDNguoiQuanLy(maNguoiQuanLy);

                    txtTenDangNhap.Text = dt.Rows[0]["TenDangNhap"].ToString();
                    //txtMatKhau.Text = dt.Rows[0]["MatKhau"].ToString();
                    txtMatKhau.Attributes.Add("value",dt.Rows[0]["MatKhau"].ToString());
                    txtHoTen.Text = dt.Rows[0]["HoTen"].ToString();
                    txtEmail.Text = dt.Rows[0]["Email"].ToString();
                    hdfMaNguoiQuanLy.Value = _maNguoiQuanLy;
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string _MaNguoiQuanLy = hdfMaNguoiQuanLy.Value;
            if (_MaNguoiQuanLy == null || _MaNguoiQuanLy == "0")
            {
                // thực hiện thêm mới
                NguoiQuanLy obj = new NguoiQuanLy();
                obj.TenDangNhap = txtTenDangNhap.Text;
                obj.MatKhau = txtMatKhau.Text;
                obj.HoTen = txtHoTen.Text;
                obj.Email = txtEmail.Text;
                _NguoiQuanLyBL.insertNguoiQuanLy(obj);
            }
            else
            {
                // thực hiện sửa
                NguoiQuanLy obj = new NguoiQuanLy();
                obj.TenDangNhap = txtTenDangNhap.Text;
                obj.MatKhau = txtMatKhau.Text;
                obj.HoTen = txtHoTen.Text;
                obj.Email = txtEmail.Text;
                obj.MaNguoiQuanLy = int.Parse(_MaNguoiQuanLy);
                _NguoiQuanLyBL.updateNguoiQuanLy(obj);
            }

            Response.Redirect("~/Admin/NguoiQuanLyManagerForm.aspx");
        }
    }
}